package com.redoor.app.stake.basicdata.service.impl;


import com.redoor.app.stake.basicdata.dao.BaseStationMapper;
import com.redoor.app.stake.basicdata.model.BaseParkingArea;
import com.redoor.app.stake.basicdata.model.BaseStation;
import com.redoor.app.stake.basicdata.model.constants.BasicDataRedisConstants;
import com.redoor.app.stake.basicdata.service.BaseStationRedisService;
import com.redoor.app.stake.common.service.RedisService;
import com.redoornetwork.framework.data.redis.util.RedisUtils;
import com.yotouch.app.stake.model.Station;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by wangjunlei on 2018/1/30.
 */
@Service
public class BaseStationRedisServiceImpl implements BaseStationRedisService {
    final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    RedisService redisService;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    BaseStationMapper baseStationMapper;

    @Override
    public boolean modifyStationInfo(String key, BaseStation stationInfo) {
        try{
            if(StringUtils.isEmpty(stationInfo.getUuid())
                    || StringUtils.isEmpty(key)
                    || !stationInfo.getUuid().equals(key)){
                logger.error("uuid is not matching station {},{}",key, stationInfo.getUuid());
                return false;
            }
            //STATION:[UUID]
            String realKey = BasicDataRedisConstants.REDIS_STATION_PRE+key;
            //删除之前缓存中的数据
            deleteStationInfo(realKey);
            //写入新数据到缓存
            redisService.add(realKey,stationInfo,BasicDataRedisConstants.REDIS_OVER_TIME_DAY);
            redisService.add(realKey+":OUTSTATIONNO",stationInfo.getOutstationno(),BasicDataRedisConstants.REDIS_OVER_TIME_DAY);
            redisService.add(realKey+":STATIONNO",stationInfo.getStationno(),BasicDataRedisConstants.REDIS_OVER_TIME_DAY);
            //STATION:[UUID]:STATIONNO
            //STATION:[UUID]:OUTSTATIONNO
            //STATION:[UUID]:STAKELIST
            //STATION:[UUID]:PARKAREALIST
        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    @Override
    public String getStationUuidByStationNo(String stationno) {
        return null;
    }

    @Override
    public boolean deleteStationInfo(String key) {
        String realKey = BasicDataRedisConstants.REDIS_STATION_PRE+key;
        if(redisService.isExists(realKey)){
            //删除站相关系列缓存
            redisUtils.removePattern(realKey);
        }
        if(redisService.isExists(BasicDataRedisConstants.REDIS_STATION_HSF_PRE+key)){
            //删除HSF接口缓存站信息
            redisUtils.removePattern(BasicDataRedisConstants.REDIS_STATION_HSF_PRE+key);
        }
        return true;
    }

    @Override
    public boolean isExistsStation(String uuid) {
        String realKey = BasicDataRedisConstants.REDIS_STATION_PRE+uuid;
        return redisService.isExists(realKey);
    }

    @Override
    public BaseStation getStationInfo(String uuid) {
        String realKey = BasicDataRedisConstants.REDIS_STATION_PRE+uuid;
        return (BaseStation)redisService.get(realKey);
    }

    @Override
    public Station getStationForHsf(String uuid) {
        return redisService.get(BasicDataRedisConstants.REDIS_STATION_HSF_PRE+uuid) == null ? null : (Station) redisService.get(BasicDataRedisConstants.REDIS_STATION_HSF_PRE+uuid);
    }

    @Override
    public boolean HSetHsfStation(List<Station> stationList) {
        for (Station station: stationList) {
            boolean flag = redisService.add(BasicDataRedisConstants.REDIS_STATION_HSF_PRE+station.getUuid(),station,BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);
            if(!flag){
                logger.error("station:{} set failed",station.getUuid());
            }
        }
        return true;
    }

    @Override
    public void removeHsfStation(String uuid) {
        if(StringUtils.isEmpty(uuid)){
            redisUtils.removePattern(BasicDataRedisConstants.REDIS_STATION_HSF_PRE);
        } else {
            redisService.delete(BasicDataRedisConstants.REDIS_STATION_HSF_PRE+uuid);
        }
    }

    @Override
    public List<BaseParkingArea> getParkingAreaList(String stationuuid) {
        //STATION:[UUID]:PARKAREALIST
        String realKey = BasicDataRedisConstants.REDIS_STATION_PRE+stationuuid+":PARKAREALIST";
        Object obj =  redisService.get(realKey);
        return obj == null ? null : (List<BaseParkingArea>) obj;
    }

    @Override
    public void setParkingAreaList(String stationuuid,List<BaseParkingArea> parkingAreas) {
        //STATION:[UUID]:PARKAREALIST
        String realKey = BasicDataRedisConstants.REDIS_STATION_PRE+stationuuid+":PARKAREALIST";
        redisService.add(realKey,parkingAreas,BasicDataRedisConstants.REDIS_OVER_TIME_HOUR);
    }

    @Override
    public void removeParkingAreaList(String stationuuid) {
        String realKey = BasicDataRedisConstants.REDIS_STATION_PRE+stationuuid+":PARKAREALIST";
        redisService.delete(realKey);
    }

    @Override
    public String getOutStationNo(String districtcode) {
        if(StringUtils.isEmpty(districtcode)){
            return null;
        }

        StringBuffer suff = new StringBuffer();
        suff.append(districtcode);
        suff.append("MA002TMQX");

        if(!redisUtils.hHasKey(BasicDataRedisConstants.STATION_OUT_NO_PRE,districtcode)){
            String outstationno  = baseStationMapper.qryMaxStationNoWithDistrictcode(districtcode);

            //获取这个地区下最大的编码加1，否则从0开始
            String str = StringUtils.isEmpty(outstationno) ? "0": outstationno.substring(suff.length());
            //组成5位编码
            Integer sub = Integer.valueOf(str);
            redisUtils.hset(BasicDataRedisConstants.STATION_OUT_NO_PRE,districtcode,sub,BasicDataRedisConstants.REDIS_OVER_TIME_DAY);
        }
        String newStationNo = String.valueOf(redisUtils.hincr(BasicDataRedisConstants.STATION_OUT_NO_PRE,districtcode,1));
        if (newStationNo.length() < 4) {
            for (int i = 0; i < 4 - newStationNo.length(); i++) {
                suff.append("0");
            }
            suff.append(newStationNo);
        }

        return suff.toString();
    }

//    @Override
//    public List<BaseParkingArea> getStakeList(String stationuuid) {
//        //STATION:[UUID]:PARKAREALIST
//        String realKey = BasicDataRedisConstants.REDIS_STATION_PRE+stationuuid+":STAKELIST";
//        Object obj =  redisService.get(realKey);
//        return obj == null ? null : (List<BaseParkingArea>) obj;
//    }
//
//    @Override
//    public List<BaseParkingArea> setStakeList(String stationuuid) {
//        //STATION:[UUID]:PARKAREALIST
//        String realKey = BasicDataRedisConstants.REDIS_STATION_PRE+stationuuid+":STAKELIST";
//        Object obj =  redisService.get(realKey);
//        return obj == null ? null : (List<BaseParkingArea>) obj;
//    }


}
